# Copyright (C) 2012 Thiago Chaves de Oliveira Horta <shundread@gmail.com>
#
# This file is part of Kapybara, and is provided to you as is under the terms
# and conditions of the GPL v3 license. The license should have been provided to
# you with this package via the file gpl-3.0.txt, but if not you can see it
# online at https://www.gnu.org/licenses/gpl-3.0.html

'''This module offers Kapybara's language-related classes.'''

class Language(object):
    def __init__(self, database, language_id):
        self.database = database
        self.id = language_id

    def name(self, language=None):
        return self.database.language_name(self, language)

    def expression_classes(self):
        return self.database.expression_classes(self)

    def expressions(self, eclass=None):
        return self.database.expressions(self, eclass)

    def functions(self, expression=None):
        return self.database.functions(self, expression)

    def translate(self, expression):
        return self.database.expression(expression, language)

    def __str__(self):
        return '<Language {0}>'.format(self.name())

class Function(object):
    def __init__(self, database, function_id):
        self.database = database
        self.id = function_id

    def name(self, language=None):
        return self.database.function_name(self, language)

    def defined_for(self, expression, language=None):
        return bool(self.database.function_evaluate(self, expression, language))

    def __call__(self, expression, language):
        return self.database.function_evaluate(self, expression, language)

    def __str__(self):
        return '<Function {0}>'.format(self.name())

class ExpressionClass(object):
    def __init__(self, database, eclass_id):
        self.database = database
        self.id = eclass_id

    def name(self, language=None):
        return self.database.expression_class_name(self, language)

    def __str__(self):
        return '<ExpressionClass {0}>'.format(self.name())

class Expression(object):
    def __init__(self, database, eclass_id, expression_id):
        self.database = database
        self.eclass_id = eclass_id
        self.id = expression_id

    def name(self, language=None):
        return self.database.expression_name(self, language)

    def __str__(self):
        return '<Expression {0}>'.format(self.name())

    def __repr__(self):
        return 'Expression({0}, {1}, {2})'.format(repr(self.database),
            self.eclass_id, self.id)

